// run: $exec < input
#include <cstdio>
#include <vector>
#include <functional>
#include <queue>
#include <cstring>

long long const inf = 0x3f3f3f3f3f3f3f;
const int maxn = 2000;

typedef std::pair<int, int> pair;

struct arc
{
	arc(int x = 0, int y = 0, int z = -1)
	{
		to = x;
		cost = y;
		next = z;
	}

	int to, cost, next;
} e[500000];

int tot, n, m, head[maxn];
long long d[maxn];
bool done[maxn];

void add (int u, int v, int cost)
{
	e[tot] = arc (v, cost, head[u]);
	head[u] = tot++;
	e[tot] = arc (u, cost, head[v]);
	head[v] = tot++;
}

std::priority_queue<pair, std::vector<pair>, std::greater<pair> > q;

long long prim ()
{
	while (!q.empty()) q.pop();
	for (int i = 1; i <= n; i++) {
		done[i] = false;
		d[i] = inf;
	}
	q.push(std::make_pair(d[1] = 0, 1));
	long long ans = 0;
	while (!q.empty()) {
		int u = q.top().second;
		q.pop();
		if (done[u]) continue;
		done[u] = true;
		ans += d[u];
		for (int i = head[u]; ~i; i = e[i].next) {
			if (!done[e[i].to] && e[i].cost < d[e[i].to]) {
				d[e[i].to] = e[i].cost;
				q.push(std::make_pair (d[e[i].to], e[i].to));
			}
		}
	}
	return ans;
}
int main ()
{
	int u, v, w;
	while (~scanf("%d %d", &n, &m)) {
		memset(head, -1, sizeof (head));
		for (int i = tot = 0; i < m; i++) {
			scanf("%d %d %d", &u, &v, &w);
			add(u, v, w);
		}
		printf("%lld\n", prim());
	}
}

